﻿using System;
using System.Collections.Generic;
using System.IO;
using Docentric.Examples.Data.BusinessObjects;
using Docentric.Examples.Data.BusinessObjects.DataModel;
using Docentric.Word;
using DTSM = Docentric.DocentricTypeSystem.Metadata;
using DTSD = Docentric.DocentricTypeSystem.Data;


namespace Docentric.Examples.Browser
{
	public class SimpleValuesDtsExample : ExampleBase
	{
		// Constructor
		public SimpleValuesDtsExample()
		{
			Name = "SimpleValuesDts";
			Title = "Simple Values (DTS)";
			Description = "In this report, we want to generate a document displaying data of a customer (customer’s name, company, job title, address etc.). The template has one (default) data source representing a 'Customer' DTS object." +
                          " The DTS object is obtained by the conversion from the 'Customer' .NET object. " +
                          "'DTS' is abbreviation of 'Docentric Type System' which represents simplified .NET type system suitable for template designing by non-technical / end users.";
		}


		// GenerateReport
		public override string GenerateReport()
		{
			// Get the customer .NET object with the Id = 15.
			Customer customer = DataAccess.GetCustomerById(15);

			// Convert to the corresponding DTS object. It is assumed that a DTS schema on the template was imported from the same .NET 'Customer' type.
			DTSD.DtsValueBase dtsCustomer = DTSM.Converters.DotNetToDtsConverter.ConvertDotNetValueToDtsValue(customer);

			// Create a temporary file for the generated report document.
			string reportDocumentFileName = System.IO.Path.GetTempPath() + "GeneratedReport_" + Guid.NewGuid().ToString() + ".docx";
			using (Stream reportDocumentStream = File.Create(reportDocumentFileName))
			{
				// Open the report template file.
				using (Stream reportTemplateStream = GetReportTemplate())
				{
					// Generate the report document using 'DocumentGenerator'.
					DocumentGenerator dg = new DocumentGenerator(dtsCustomer);
					DocumentGenerationResult result = dg.GenerateDocument(reportTemplateStream, reportDocumentStream);
					if (result.HasErrors)
					{
						foreach (Word.Error error in result.Errors) Console.Out.WriteLine(error.Message);
					}
				}
			}

			return reportDocumentFileName;
		}
	}
}
